﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Model_SSResourceManager
{
    class ResourceDataContext: ItemDataContext <Resource ,RESOURCE>
    {
        public ResourceDataContext()
        {
            EntitySetName = "RESOURCES";
        }

        public List<Resource> GetActiveResources()
        {
            List<Resource> _retList = new List<Resource>();

            using (SSRMDBEntities context = new SSRMDBEntities())
            {
                try
                {                   
                    Resource modelItem;
                    foreach (RESOURCE entItem in (from entResource in context.RESOURCES
                                                  where !entResource.Locked select entResource) 
                            )
                       {
                           modelItem = new Resource();
                           EntityToModel(entItem, modelItem);
                           _retList.Add(modelItem);
                       }
                }
                catch (Exception ex)
                {
                    OnRaiceDbErrEvent(new DbErrEventArgs(ex.Message));
                }
            }

            return _retList;
        }

        protected override void EntityToModel(RESOURCE _entResource, Resource _Resource)
        {
            _Resource.Id     = _entResource.Id;
            _Resource.Name   = _entResource.Name;
            _Resource.StartTime = _entResource.StartTime;
            _Resource.EndTime = _entResource.EndTime;
            _Resource.Locked = _entResource.Locked ;
            _Resource.TimeOfUsing = _entResource.TimeOfUsing ;
            _Resource.UserCount = _entResource.UserCount ;
            _Resource.OnlyAutoTime = _entResource.OnlyAutoTime;            
        }

        protected override void ModelToEntity(Resource _Resource, RESOURCE _entResource)
        {
            _entResource.Id = _Resource.Id;
            _entResource.Name = _Resource.Name;
            _entResource.StartTime = _Resource.StartTime;
            _entResource.EndTime = _Resource.EndTime;
            _entResource.Locked = _Resource.Locked;
            _entResource.TimeOfUsing = _Resource.TimeOfUsing;
            _entResource.UserCount = _Resource.UserCount;
            _entResource.OnlyAutoTime = _Resource.OnlyAutoTime;  
        }
    }
}
